import Router, { RouteConfig } from 'vue-router'
import App from '../App.vue'
import Home from '../pages/home/index.vue'
import Login from '../pages/login/index.vue'
import HomeChannel from '../pages/home/channel/index.vue'
import HomeChannelSalesman from '../pages/home/channel-salesman/index.vue'
import Memberships from '~/pages/memberships/index.vue'
import MembershipsProcess0 from '~/pages/memberships/process-0/index.vue'
import MembershipsProcess1 from '~/pages/memberships/process-1/index.vue'
import MembershipsProcess2 from '~/pages/memberships/process-2/index.vue'
import MembershipsProcess3 from '~/pages/memberships/process-3/index.vue'
import MembershipsProcess4 from '~/pages/memberships/process-4/index.vue'

import UserCenter from '~/pages/user-center/index.vue'
import Base from '~/pages/user-center/base/index.vue'
import BaseRegister from '~/pages/user-center/base/base-register/index.vue'
import Safe from '~/pages/user-center/safe/index.vue'
import SafePassword from '~/pages/user-center/safe/password/index.vue'
import SafePhone from '~/pages/user-center/safe/phone/index.vue'
import Auth from '~/pages/user-center/auth/index.vue'
import AuthInfo from '~/pages/user-center/auth/info/index.vue'

import Salesman from '~/pages/salesman/index.vue'
import SalesmanList from '~/pages/salesman/salesman-list/index.vue'
import SalesmanDetail from '~/pages/salesman/salesman-detail/index.vue'
import Vue from 'vue'

import Message from '~/pages/message/index.vue'
import MessageBill from '~/pages/message/bill/index.vue'
import MessageTrans from '~/pages/message/trans/index.vue'
import MessageSystem from '~/pages/message/system/index.vue'

import ForgetPassword from '~/pages/forget-password/index.vue'
import Rank from '../pages/home/rank/index.vue'

const routes: RouteConfig[] = [
  {
    path: '/forget-password',
    component: ForgetPassword,
    name: 'forget-password'
  },
  {
    path: '/',
    component: App,
    name: 'app',
    redirect: '/login',
    children: [
      {
        path: 'home',
        component: Home,
        name: '主页',
        children: [
          {
            path: 'channel',
            component: HomeChannel,
            meta: {
              activeRoot: '/home/channel'
            }
          },
          {
            path: 'channel-salesman',
            component: HomeChannelSalesman,
            meta: {
              activeRoot: '/home/channel-salesman'
            }
          },
          {
            path: 'rank/:id',
            name: '排行榜',
            component: Rank,
            meta: {
              activeRoot: '/home/rank'
            }
          }
        ]
      },
      // {
      //   path: 'memberships',
      //   component: Memberships
      // },
      // {
      //   path: 'salesman',
      //   component: Salesman,
      //   redirect: '/salesman/list',
      //   children: [
      //     {
      //       path: 'list',
      //       component: SalesmanList,
      //       meta: {
      //         activeRoot: '/salesman'
      //       }
      //     },
      //     {
      //       path: 'detail/:id',
      //       component: SalesmanDetail
      //     }
      //   ]
      // },
      {
        path: 'user-center',
        name: '用户中心',
        component: UserCenter,
        redirect: '/user-center/base',
        children: [
          {
            path: 'base',
            component: Base,
            redirect: '/user-center/base/base-register',
            children: [
              {
                path: 'base-register',
                component: BaseRegister,
                meta: {
                  activeRouter: '/user-center/base'
                }
              }
            ]
          },
          {
            path: 'safe',
            component: Safe,
            redirect: '/user-center/safe/password',
            children: [
              {
                path: 'password',
                component: SafePassword,
                meta: {
                  activeRouter: '/user-center/safe',
                  activeChildren: '/user-center/safe/password'
                }
              },
              {
                path: 'phone',
                component: SafePhone,
                meta: {
                  activeRouter: '/user-center/safe',
                  activeChildren: '/user-center/safe/phone'
                }
              }
            ]
          },
          {
            path: 'auth',
            component: Auth,
            redirect: '/user-center/auth/info',
            children: [
              {
                path: 'info',
                component: AuthInfo,
                meta: {
                  activeRouter: '/user-center/auth',
                  activeChildren: '/user-center/auth/info'
                }
              }
            ]
          }
        ]
      },
      {
        path: 'member',
        name: '企业管理',
        component: () => import(/* webpackChunkName: "platform" */ '~/pages/member/index.vue'),
        redirect: '/member/list',
        children: [
          {
            path: 'list',
            component: () => import(/* webpackChunkName: "platform" */ '~/pages/member/list/index.vue'),
            meta: {
              activeRoot: '/member'
            }
          },
          {
            path: 'detail/:id',
            component: () => import(/* webpackChunkName: "platform" */ '~/pages/member/detail/index.vue'),
            meta: {
              activeRoot: '/member'
            }
          }
        ]
      },
      {
        path: 'salesmen',
        name: '业务员管理',
        component: () => import(/* webpackChunkName: "platform" */ '~/pages/salesmen/index.vue'),
        redirect: '/salesmen/list',
        children: [
          {
            path: 'list',
            component: () => import(/* webpackChunkName: "platform" */ '~/pages/salesmen/list/index.vue'),
            meta: {
              activeRoot: '/salesmen'
            }
          },
          {
            path: 'detail/:id',
            component: () => import(/* webpackChunkName: "platform" */ '~/pages/salesmen/detail/index.vue'),
            meta: {
              activeRoot: '/salesmen'
            }
          }
        ]
      },
      {
        name: '消息提醒',
        path: 'message',
        redirect: '/message/bill',
        component: Message,
        children: [
          {
            name: '票据提醒',
            path: 'bill',
            component: MessageBill,
            meta: '/message/bill'
          },
          {
            name: '交易提醒',
            path: 'trans',
            component: MessageTrans,
            meta: '/message/trans'
          },
          {
            name: '系统提醒',
            path: 'system',
            component: MessageSystem,
            meta: '/message/system'
          }
        ]
      }
    ]
  },
  {
    path: '/login',
    name: 'login',
    component: Login
  },
  {
    path: '/register',
    name: 'register',
    component: () => import(/* webpackChunkName: "register" */ '~/pages/register/index.vue'),
    children: [
      {
        path: 'guide',
        component: () => import(/* webpackChunkName: "register" */ '~/pages/register/guide/index.vue')
      },
      {
        path: 'process-0',
        component: () => import(/* webpackChunkName: "register" */ '~/pages/register/process-0/index.vue')
      },
      {
        path: 'process-1',
        component: () => import(/* webpackChunkName: "register" */ '~/pages/register/process-1/index.vue')
      },
      {
        path: 'process-2',
        component: () => import(/* webpackChunkName: "register" */ '~/pages/register/process-2/index.vue')
      },
      {
        path: 'process-3',
        component: () => import(/* webpackChunkName: "register" */ '~/pages/register/process-3/index.vue')
      },
      {
        path: 'process-4',
        component: () => import(/* webpackChunkName: "register" */ '~/pages/register/process-4/index.vue')
      },
      {
        path: 'process-5',
        component: () => import(/* webpackChunkName: "register" */ '~/pages/register/process-5/index.vue')
      }
    ]
  }
]
export const router: Router = new Router({
  routes,
  mode: 'history',
  scrollBehavior () {
    return {
      x: 0,
      y: 0
    }
  }
})

let freePage = ['login', 'register', 'forget-password']
router.beforeEach((to, form, next) => {
  let token = window.sessionStorage.getItem('token')
  if (token) {
    next()
  } else {
    let currentRouter = to.fullPath.split('/')[1]
    if (freePage.indexOf(currentRouter) === -1) {
      Vue.prototype.$notify({
        title: '提示',
        message: '您的登录信息已失效,请重新登录',
        type: 'error'
      })
      next('/login')
    } else {
      next()
    }
  }
})
